<template>
  <el-form
    ref="formRef"
    :model="formData"
    :rules="formRules"
    v-loading="formLoading"
    label-width="0px"
    :inline-message="true"
  >
    <el-table :data="formData" class="-mt-10px">
      <el-table-column label="序号" type="index" width="100" />
      <el-table-column label="危险源/重点消防部位 类别" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.type`" :rules="formRules.type" class="mb-0px!">
            <el-select filterable v-model="row.type" placeholder="请选择危险源/重点消防部位 类别">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.CRM_DANGER_FIRE_TYPE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="危险源管理责任人" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.manager`" :rules="formRules.manager" class="mb-0px!">
            <el-input v-model="row.manager" placeholder="请输入危险源管理责任人" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="联系电话" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item
            :prop="`${$index}.phoneNumber`"
            :rules="formRules.phoneNumber"
            class="mb-0px!"
          >
            <el-input v-model="row.phoneNumber" placeholder="请输入联系电话" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="安全措施" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.measure`" :rules="formRules.measure" class="mb-0px!">
            <el-select multiple filterable v-model="row.measure" placeholder="请选择安全措施">
              <el-option
                v-for="dict in getIntDictOptions(DICT_TYPE.CRM_SAFE_MEASURE)"
                :key="dict.value"
                :label="dict.label"
                :value="dict.value"
              />
            </el-select>
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column label="其他安全措施/备注" min-width="150">
        <template #default="{ row, $index }">
          <el-form-item :prop="`${$index}.note`" :rules="formRules.note" class="mb-0px!">
            <el-input v-model="row.note" placeholder="请输入其他安全措施/备注" />
          </el-form-item>
        </template>
      </el-table-column>
      <el-table-column align="center" fixed="right" label="操作" width="60">
        <template #default="{ $index }">
          <el-button @click="handleDelete($index)" link circle
            ><Icon icon="ep:remove" color="red"
          /></el-button>
        </template>
      </el-table-column>
    </el-table>
  </el-form>
  <el-row justify="center" class="mt-3">
    <el-button type="primary" plain @click="handleAdd" round
      ><Icon icon="ep:plus" class="mr-2" />增加行</el-button
    >
  </el-row>
</template>
<script setup lang="ts">
import { TechDefenseApi } from '@/api/crm/tech_defense'
import { getIntDictOptions, DICT_TYPE } from '@/utils/dict'
const props = defineProps<{
  defenseId: undefined // 危险源编号（主表的关联字段）
}>()
const formLoading = ref(false) // 表单的加载中
const formData = ref([])
const formRules = reactive({
  dangerId: [{ required: true, message: '危险源编号不能为空', trigger: 'blur' }],
  type: [{ required: true, message: '危险源/重点消防部位 类别不能为空', trigger: 'change' }],
  manager: [{ required: true, message: '危险源管理责任人不能为空', trigger: 'blur' }],
  phoneNumber: [{ required: true, message: '联系电话不能为空', trigger: 'blur' }],
  measure: [{ required: true, message: '安全措施不能为空', trigger: 'change' }]
})
const formRef = ref() // 表单 Ref

/** 监听主表的关联字段的变化，加载对应的子表数据 */
watch(
  () => props.defenseId,
  async (val) => {
    // 1. 重置表单
    formData.value = []
    // 2. val 非空，则加载数据
    if (!val) {
      return
    }
    try {
      formLoading.value = true
      formData.value = await TechDefenseApi.getTechDefenseManageListByDefenseId(val)
    } finally {
      formLoading.value = false
    }
  },
  { immediate: true }
)

/** 新增按钮操作 */
const handleAdd = () => {
  const row = {
    id: undefined,
    defenseId: undefined,
    type: undefined,
    manager: undefined,
    phoneNumber: undefined,
    measure: undefined,
    note: undefined
  }
  row.defenseId = props.defenseId
  formData.value.push(row)
}

/** 删除按钮操作 */
const handleDelete = (index) => {
  formData.value.splice(index, 1)
}

/** 表单校验 */
const validate = () => {
  return formRef.value.validate()
}

/** 表单值 */
const getData = () => {
  return formData.value
}

defineExpose({ validate, getData })
</script>
